package exchange;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.concurrent.BlockingQueue;

import message.Message;

public class MessageSender implements Runnable {
	private BlockingQueue<Message> outQueue;
	private HashMap<String, ExchangeClient> clientMap;

	public MessageSender(BlockingQueue<Message> outQueue,
			HashMap<String, ExchangeClient> clientMap) {
		super();
		this.outQueue = outQueue;
		this.clientMap = clientMap;
	}

	@Override
	public void run() {
		try {
			while (true) {
				Message message = outQueue.take();
				String clientID = message.getClientId();
				ObjectOutputStream oos = clientMap.get(clientID).getOut();
				oos.writeObject(message.toString());
				oos.reset();
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
